programming4us
           
 
 
SQL Server

Programming with SQL Azure : Connecting to SQL Azure (part 1) - ADO.NET

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
1/2/2011 11:54:40 AM
Developing applications that work with SQL Azure isn't rocket science, but it requires knowing what to expect and what functionality you have to work with. You read earlier that not all client libraries work with SQL Azure and saw the libraries that are supported.

This section looks at using several technologies to connect to and query a SQL Azure database, including ADO.NET, ODBC, and WCF Data Services. You read at length earlier about taking the right approach to move to the Azure platform. You must consider many things, including the following:

  • SQL Azure is only available via TCP port 1433.

  • SQL Azure doesn't currently support OLE DB.

  • SQL Azure only supports SQL Server authentication. Windows Authentication isn't supported.

  • When connecting to SQL Azure, you must specify the target database in the connection string. Otherwise, you're connecting to the master database.

  • Distributed transactions (transactions that affect multiple resources, such as tables, or different databases via sharding) aren't supported in SQL Azure.

  • You must ensure that your SQL Azure firewall is configured to accept connections.

  • You must determine whether any embedded (in-line) T-SQL in your application is supported by SQL Azure.

  • You must use the login name format <login>@<server> when connecting to SQL Azure, because some tools implement Tabular Data Stream (TDS) differently.

Let's get to some coding. The next few sections show you how to connect to SQL Azure using different libraries such as ADO.NET, ODBC, the sqlcmd utility, and WCF Data Services to query SQL Azure.

1. ADO.NET

Microsoft makes it very easy to connect an application to SQL Azure by providing the necessary connection strings for both ADO.NET and ODBC, as shown in Figure 1. You can find the connection information on the SQL Azure Server Administration page by selecting a database and clicking the Connection Strings button.

Figure 1. Connection strings

1.1. Making the Connection

Let's first look at how to connect to an Azure database using ADO.NET. Fire up an instance of Visual Studio 2010, and create a new Windows Forms application. Then, follow these steps:

  1. Place a button on Form1, and double-click the new button to view its click event.

  2. Before you place any code in the click event, let's add a method to get a connection string. To demonstrate connecting to SQL Azure versus a local database, let's first connect to a local copy of the database. Then, you can change to connect to Azure. Below the click event, add a new method called GetConString that returns the connection string for your local instance of SQL Server. Here's the code to write:

    string GetConString()
    {
    return "Server=server;Database=TechBio;User ID=sa;Password=password;";
    }

  3. Go back to the button's click event, and add the following code. This code calls the GetConString method you previously added, returns the connection string, establishes and opens a connection to the local database, and then closes the connection:

    private void button1_Click(object sender, EventArgs e)
    {
    string connStr = GetConString();
    using (SqlConnection conn = new SqlConnection(connStr))
    {
    try
    {
    conn.Open();
    MessageBox.Show("Connection made.");
    }
    catch (SqlException ex)
    {
    MessageBox.Show(ex.Message.ToString());
    }
    finally
    {
    conn.Close();
    }
    }
    }

  4. Run the application, and click the button on the form. You should get a message box that says "Connection Made."

Now, let's change this simple application to connect to SQL Azure. Instead of returning the connection string to your local database, you want to return the ADO.NET connection string. Continue as follows:

  1. On your SQL Azure Server Administration page, select the database you want to connect to.

  2. Click the Connection Strings button, and click the Copy to Clipboard link for the ADO.NET connection string.

  3. Back in your Visual Studio project, replace the local connection string in the GetConString method with the SQL Azure ADO.NET connection string, as shown in the following code. Be sure to enter your correct password into the connection string:

    string GetConString()
    {
    return "Server=tcp:servername.database.windows.net;Database=TechBio;
    UserID=SQLScott@servername;Password=password;
    Trusted_Connection=False;Encrypt=True;";
    }

  4. Before you run the application, make sure your Azure firewall settings are up to date (via the SQL Azure Server Administration page). Then, run the application, and click the button on the form. If everything is configured correctly, you should get a message box that says "Connection Made."

Granted, this is a very simple example, but it illustrates how easy it is to take an existing application and point it to SQL Azure. The caveat is what your application contains. As mentioned earlier, if you have any inline T-SQL, you at a minimum need to ensure that your inline T-SQL is supported by SQL Azure. The likelihood is that it is, but it's always safest to check and test.

Even though you've connected to SQL Azure, does that affect your data-access code? The next two sections discuss using a data reader and a dataset when connecting to SQL Azure.

Other -----------------
- Programming with SQL Azure : Application Deployment Factors
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 3)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 2)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 1)
- SQL Server 2008: SQL Server Web Services
- SQL Server 2008: SQL Server Service Broker - Related System Catalogs
- SQL Azure Backup Strategies (part 2)
- SQL Azure Backup Strategies (part 1) - Copying a Database
- SQL Server 2008: Troubleshooting SSB Applications with ssbdiagnose.exe
- SQL Server 2008: Service Broker Routing and Security
- Migrating Databases and Data to SQL Azure (part 9)
- Migrating Databases and Data to SQL Azure (part 8)
- Understanding Service Broker Constructs (part 5)
- Understanding Service Broker Constructs (part 4) - Creating the Conversation Initiator
- Migrating Databases and Data to SQL Azure (part 7)
- Migrating Databases and Data to SQL Azure (part 6) - Building a Migration Package
- Migrating Databases and Data to SQL Azure (part 5) - Creating an Integration Services Project
- Understanding Service Broker Constructs (part 3)
- Understanding Service Broker Constructs (part 2) - Creating Queues for Message Storage
- Understanding Service Broker Constructs (part 1) - Defining Messages and Choosing a Message Type
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us